﻿Imports DoAnQuanLyBanHang1065043_1065007.DoAnQuanLyBanHang.General
Imports DoAnQuanLyBanHang1065043_1065007.DoAnQuanLyBanHang.DAO
Imports DevComponents.DotNetBar

Public Class frmNhanVien
    Private m_QuyDinh As New QuyDinh()
    Private m_NhanVienCtrl As New NhanVienCtrl()
    Private m_QuayGiaoDihCtrl As New QuayGiaoDichCtrl()
    Public Sub New()
        InitializeComponent()
        DataService.OpenConnection()
    End Sub


    Private Sub frmNhanVien_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        m_QuayGiaoDihCtrl.HienThiComboBox(cmbQuayGiaoDich)
        m_QuayGiaoDihCtrl.HienThiDataGridViewComboBoxColumn(colMaQuayGD)
        m_NhanVienCtrl.HienThi(DGVNhanVien, BindingNavigatorNhanVien, txtMaNV, txtHoTen, dtNgaySinh, txtDiaChi, _
         txtSoDienThoai, cmbQuayGiaoDich)

    End Sub

    Private Sub bindingNavigatorRefreshItem_Click(sender As System.Object, e As System.EventArgs) Handles bindingNavigatorRefreshItem.Click
        txtTimKiem.Text = ""
        m_QuayGiaoDihCtrl.HienThiComboBox(cmbQuayGiaoDich)
        m_QuayGiaoDihCtrl.HienThiDataGridViewComboBoxColumn(colMaQuayGD)
        m_NhanVienCtrl.HienThi(DGVNhanVien, BindingNavigatorNhanVien, txtMaNV, txtHoTen, dtNgaySinh, txtDiaChi, _
         txtSoDienThoai, cmbQuayGiaoDich)

    End Sub

    Private Sub bindingNavigatorAddNewItem_Click(sender As System.Object, e As System.EventArgs) Handles bindingNavigatorAddNewItem.Click
        If DGVNhanVien.RowCount = 0 Then
            BindingNavigatorNhanVien.Enabled = True
        End If
        Dim m_Row As DataRow = m_NhanVienCtrl.ThemDongMoi()
        m_Row("MaNV") = "NV" & m_QuyDinh.LaySTT(DGVNhanVien.Rows.Count + 1)
        m_Row("HoTenNV") = ""
        m_Row("NgaySinh") = DateTime.Today
        m_Row("DiaChi") = ""
        m_Row("DienThoai") = ""
        m_Row("MaQuayGiaoDich") = ""

        m_NhanVienCtrl.ThemMoi(m_Row)
        BindingNavigatorNhanVien.BindingSource.MoveLast()

    End Sub

    Public Function KiemTraTruocKhiLuu(str As [String]) As [Boolean]
        For Each row As DataGridViewRow In DGVNhanVien.Rows
            If row.Cells(str).Value IsNot Nothing Then
                Dim m_Str As [String] = row.Cells(str).Value.ToString()
                If m_Str = "" Then
                    Return False
                End If
            End If
        Next
        Return True
    End Function


    Private Sub bindingNavigatorSaveItem_Click(sender As System.Object, e As System.EventArgs) Handles bindingNavigatorSaveItem.Click
        Try
            If KiemTraTruocKhiLuu("colMaNV") = True AndAlso KiemTraTruocKhiLuu("colHoTen") = True AndAlso KiemTraTruocKhiLuu("colDiaChi") = True AndAlso KiemTraTruocKhiLuu("colDienThoai") = True AndAlso KiemTraTruocKhiLuu("colNgaySinh") = True AndAlso KiemTraTruocKhiLuu("colMaQuayGD") = True Then
                bindingNavigatorPositionItem.Focus()
            End If
            m_NhanVienCtrl.LuuNhanVien()
        Catch
            MessageBoxEx.Show("Giá trị ô không được rỗng!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        End Try

    End Sub

    Private Sub bindingNavigatorDeleteItem_Click(sender As System.Object, e As System.EventArgs) Handles bindingNavigatorDeleteItem.Click
        If DGVNhanVien.RowCount = 0 Then
            bindingNavigatorDeleteItem.Enabled = False

        ElseIf MessageBox.Show("Bạn có chắc chắn xóa dòng này không?", "Xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            BindingNavigatorNhanVien.BindingSource.RemoveCurrent()
        End If

    End Sub

    Private Sub bindingNavigatorCloseItem_Click(sender As System.Object, e As System.EventArgs) Handles bindingNavigatorCloseItem.Click
        Me.Close()

    End Sub

    Private Sub btnThem_Click(sender As System.Object, e As System.EventArgs) Handles btnThem.Click
        LoadMain.HienThiQuayGiaoDich()

    End Sub

#Region "Tim Kiem nhan vien"
    Private Sub btnSearch_KeyDown(sender As Object, e As KeyEventArgs)
        If e.KeyCode = Keys.Enter Then
            TimKiemNhanVien()
        End If
    End Sub

    Private Sub TimKiemNhanVien()
        Try
            If cbMaNV.Checked = True Then
                m_NhanVienCtrl.TimKiemTheoMa(txtTimKiem.Text)
            ElseIf cbHoTen.Checked = True Then
                m_NhanVienCtrl.TiemKiemTheoTen(txtTimKiem.Text)
            End If
        Catch
            MessageBoxEx.Show("Không thể tìm thấy Giá trị cần tìm!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        End Try
    End Sub

    Private Sub btnSearch_Click(sender As System.Object, e As System.EventArgs) Handles btnSearch.Click
        If txtTimKiem.Text = "" Then
            MessageBoxEx.Show("Giá trị tìm rỗng!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        Else
            TimKiemNhanVien()
        End If

    End Sub
#End Region

    Private Sub btnThemNV_Click(sender As System.Object, e As System.EventArgs) Handles btnThemNV.Click
        If DGVNhanVien.RowCount = 0 Then
            BindingNavigatorNhanVien.Enabled = True
        End If
        Dim m_Row As DataRow = m_NhanVienCtrl.ThemDongMoi()
        m_Row("MaNV") = "NV" & m_QuyDinh.LaySTT(DGVNhanVien.Rows.Count + 1)
        m_Row("HoTenNV") = ""
        m_Row("NgaySinh") = DateTime.Today
        m_Row("DiaChi") = ""
        m_Row("DienThoai") = ""
        m_Row("MaQuayGiaoDich") = ""

        m_NhanVienCtrl.ThemMoi(m_Row)
        BindingNavigatorNhanVien.BindingSource.MoveLast()
    End Sub

    Private Sub btnLuu_Click(sender As System.Object, e As System.EventArgs) Handles btnLuu.Click
        Try
            If KiemTraTruocKhiLuu("colMaNV") = True AndAlso KiemTraTruocKhiLuu("colHoTen") = True AndAlso KiemTraTruocKhiLuu("colDiaChi") = True AndAlso KiemTraTruocKhiLuu("colDienThoai") = True AndAlso KiemTraTruocKhiLuu("colNgaySinh") = True AndAlso KiemTraTruocKhiLuu("colMaQuayGD") = True Then
                bindingNavigatorPositionItem.Focus()
            End If
            m_NhanVienCtrl.LuuNhanVien()
            MessageBoxEx.Show("Đã lưu thành công!", "Hoàn Thành", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch
            MessageBoxEx.Show("Phải nhập đầy đủ dữ liệu để thêm nhân viên", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        End Try

    End Sub

    Private Sub btnHuy_Click(sender As System.Object, e As System.EventArgs) Handles btnHuy.Click
        Me.Close()

    End Sub

    Private Sub btnSua_Click(sender As System.Object, e As System.EventArgs) Handles btnSua.Click
        m_NhanVienCtrl.SuaNhanVien(txtMaNV.Text, txtHoTen.Text, dtNgaySinh.Value, txtDiaChi.Text, txtSoDienThoai.Text, cmbQuayGiaoDich.SelectedValue.ToString())
        m_NhanVienCtrl.HienThi(DGVNhanVien, BindingNavigatorNhanVien, txtMaNV, txtHoTen, dtNgaySinh, txtDiaChi, _
         txtSoDienThoai, cmbQuayGiaoDich)
        MessageBoxEx.Show("Đã sửa thành công!", "Hoàn Thành", MessageBoxButtons.OK, MessageBoxIcon.Information)

    End Sub

    Private Sub btnDel_Click(sender As System.Object, e As System.EventArgs) Handles btnDel.Click
        If MessageBoxEx.Show("Bạn có chắc chắn xóa nhân viên này không?", "Xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            m_NhanVienCtrl.XoaNhanVien(txtMaNV.Text)
            m_NhanVienCtrl.HienThi(DGVNhanVien, BindingNavigatorNhanVien, txtMaNV, txtHoTen, dtNgaySinh, txtDiaChi, _
             txtSoDienThoai, cmbQuayGiaoDich)
        End If

    End Sub
End Class